<?php

namespace App\Http\Resources\Index\Summary;

use App\Http\Resources\Resource;
use App\Http\Resources\Traits\Attributes;
use Helper\Timed;

class DataResource extends Resource
{
    use Attributes;

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $amounts = array_column($this->resource->toArray(), 'amount', 'timed');

        $timedEnd = Timed::getTodayTimed();
        $timed = $timedEnd - 86400 * 6;

        for (; $timed <= $timedEnd; $timed += 86400) {
            $charts['timed'][] = date('Y.m.d', $timed);
            $charts['data'][] = $amounts[$timed] ?? 0;
        }

        $this->setAttribute('chart', $charts);

        return $this->getResponse()->setResponseData($this->getAttributes());
    }
}
